package pers.myz.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import pers.myz.common.QueryPageParam;
import pers.myz.common.Result;
import pers.myz.entity.SnsBlogComment;
import pers.myz.entity.SnsIrQuestion;
import pers.myz.service.SnsBlogCommentService;
import pers.myz.service.SnsIrQuestionService;

import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lzw
 * @since 2023-07-01
 */
@RestController
@RequestMapping("/sns-ir-question")
public class SnsIrQuestionController {

    @Autowired
    private SnsIrQuestionService snsIrQuestionService;
    //新增
    @PostMapping("/save")
    public Result save(@RequestBody SnsIrQuestion irQuestion){
        return snsIrQuestionService.save(irQuestion)?Result.suc():Result.fail();
    }

    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String messageId = (String)param.get("messageId");

        Page<SnsIrQuestion> page = new Page();
        page.setCurrent(query.getQuestionNum());
        page.setSize(query.getQuestionSize());

        LambdaQueryWrapper<SnsIrQuestion> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.orderByDesc(SnsIrQuestion::getCreateTime);
        if(StringUtils.isNotBlank(messageId) && !"null".equals(messageId)){
            lambdaQueryWrapper.eq(SnsIrQuestion::getMessageId,messageId);
        }
        IPage result = snsIrQuestionService.pageC(page,lambdaQueryWrapper);
        return Result.suc(result.getRecords(),result.getTotal());
    }

}
